/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Emulator.java
 *
 * Created on 08-oct-2011, 15:19:49
 */
package gui.ventanas;

import automatas.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Arturo
 */
public class Emulator2 extends javax.swing.JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    static int temp = 0;
    int numero;
    int actual = 0;
    EmuladorFA aEmular;
    Dibujador dibujador;

    /** Creates new form Emulator */
    public Emulator2() {
        initComponents();
        temp++;
        numero = temp;
        this.cadena.setText("");
    }

    public void setFA(EmuladorFA automata) {
        aEmular = automata;
        dibujador = new Dibujador();
        dibujador.cargarFromDA(aEmular);
        //dibujador.ejecutar("temp__" + numero + "__" + actual + ".jpg");
        ejecutarYsetear();
        //System.out.println(dibujador.getOutput() + "temp__" + numero + "__" + actual + ".jpg");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cadena = new javax.swing.JTextField();
        nextNFA = new javax.swing.JButton();
        previusNFA = new javax.swing.JButton();
        playNFA = new javax.swing.JButton();
        graficadorFA1 = new gui.ventanas.GraficadorFA();
        jbParar = new javax.swing.JButton();
        labelAceptada = new javax.swing.JLabel();
        lpost = new javax.swing.JLabel();
        lact = new javax.swing.JLabel();
        lpre = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gui.ventanas.GrafosVentanasApp.class).getContext().getResourceMap(Emulator2.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        cadena.setText(resourceMap.getString("cadena.text")); // NOI18N
        cadena.setToolTipText(resourceMap.getString("cadena.toolTipText")); // NOI18N
        cadena.setName("cadena"); // NOI18N

        nextNFA.setText(resourceMap.getString("nextNFA.text")); // NOI18N
        nextNFA.setEnabled(false);
        nextNFA.setName("nextNFA"); // NOI18N
        nextNFA.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nextNFAMouseClicked(evt);
            }
        });

        previusNFA.setText(resourceMap.getString("previusNFA.text")); // NOI18N
        previusNFA.setEnabled(false);
        previusNFA.setName("previusNFA"); // NOI18N
        previusNFA.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                previusNFAMouseClicked(evt);
            }
        });

        playNFA.setText(resourceMap.getString("playNFA.text")); // NOI18N
        playNFA.setName("playNFA"); // NOI18N
        playNFA.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                playNFAMouseClicked(evt);
            }
        });

        graficadorFA1.setName("graficadorFA1"); // NOI18N

        jbParar.setText(resourceMap.getString("jbParar.text")); // NOI18N
        jbParar.setEnabled(false);
        jbParar.setName("jbParar"); // NOI18N
        jbParar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbPararMouseClicked(evt);
            }
        });

        labelAceptada.setText(resourceMap.getString("labelAceptada.text")); // NOI18N
        labelAceptada.setName("labelAceptada"); // NOI18N

        lpost.setText(resourceMap.getString("lpost.text")); // NOI18N
        lpost.setName("lpost"); // NOI18N

        lact.setFont(resourceMap.getFont("lact.font")); // NOI18N
        lact.setText(resourceMap.getString("lact.text")); // NOI18N
        lact.setName("lact"); // NOI18N

        lpre.setText(resourceMap.getString("lpre.text")); // NOI18N
        lpre.setName("lpre"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(graficadorFA1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cadena, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(playNFA, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbParar, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelAceptada)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 290, Short.MAX_VALUE)
                        .addComponent(lpre)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lact)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lpost)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(previusNFA)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nextNFA)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cadena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(graficadorFA1, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nextNFA)
                    .addComponent(playNFA)
                    .addComponent(previusNFA)
                    .addComponent(jbParar)
                    .addComponent(lpost)
                    .addComponent(lact)
                    .addComponent(lpre)
                    .addComponent(labelAceptada))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void playNFAMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_playNFAMouseClicked

        if (cadena.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Ingrese una cadena para emular");
            return;
        }
        this.iniciar();
    }//GEN-LAST:event_playNFAMouseClicked

    private void ejecutarYsetear() {
        dibujador.ejecutar("temp__" + numero + "__" + actual + aEmular.getActuales() + ".jpg");
        Icon imagen = new ImageIcon(dibujador.getOutput() + "temp__" + numero + "__" + actual + aEmular.getActuales() + ".jpg");
        graficadorFA1.jLabel1.setIcon(imagen);

    }

    private void iniciar() {
        actual = 0;
        ejecutarYsetear();
        aEmular.reiniciar();

        jbParar.setEnabled(true);
        playNFA.setEnabled(false);
        previusNFA.setEnabled(false);
        nextNFA.setEnabled(true);
        cadena.setEnabled(false);

        if (aEmular.acepta(cadena.getText())) {
            labelAceptada.setText("Cadena Aceptada");
        } else {
            labelAceptada.setText("Cadena no Aceptada");
        }
        actualizarLabel();
    }

    private void parar() {
        jbParar.setEnabled(false);
        playNFA.setEnabled(true);
        previusNFA.setEnabled(false);
        nextNFA.setEnabled(false);
        cadena.setEnabled(true);

    }

    private void anterior() {
        if (actual == 0){
            previusNFA.setEnabled(false);
            return;
            
        }
        actual--;

        aEmular.retroceder();
        ejecutarYsetear();



        if (actual == 0) {
            previusNFA.setEnabled(false);
            //nextNFA.setEnabled();
        }
        if (actual != cadena.getText().length()) {
            nextNFA.setEnabled(true);
        }
        actualizarLabel();
        // System.out.println(actual + " - " + cadena.getText().charAt(actual));
    }

    private void actualizarLabel() {
        if (actual == 0) 
            lpre.setText("");
        else 
            lpre.setText(cadena.getText().substring(0, actual));
        
        lact.setText(cadena.getText().charAt(actual) + "");
        
        if (actual != cadena.getText().length())
            lpost.setText(cadena.getText().substring(actual + 1));
        else
            lpost.setText("");
    }

    private void siguiente() {
        //System.out.println("enra: " + actual + " - " + cadena.getText().charAt(actual));

        actual++;

        boolean avanzo = aEmular.avanzar(cadena.getText().charAt(actual - 1));
        if (avanzo) {
            //System.out.println(aEmular.getActuales());
            ejecutarYsetear();
        } else {
            nextNFA.setEnabled(false);
        }
        actualizarLabel();
        //System.out.println(cadena.getText().length() - actual);
        System.out.println(actual + " de " + cadena.getText().length());

        if (actual == cadena.getText().length() - 1) {
            nextNFA.setEnabled(false);
        }
        if (actual != 0) {
            previusNFA.setEnabled(true);
        }
        // System.out.println("sale " + actual + " - " + cadena.getText().charAt(actual));
    }
    private void nextNFAMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nextNFAMouseClicked
        this.siguiente();
    }//GEN-LAST:event_nextNFAMouseClicked

    private void jbPararMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbPararMouseClicked
        this.parar();
    }//GEN-LAST:event_jbPararMouseClicked

    private void previusNFAMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_previusNFAMouseClicked
        anterior();
    }//GEN-LAST:event_previusNFAMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cadena;
    private gui.ventanas.GraficadorFA graficadorFA1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton jbParar;
    private javax.swing.JLabel labelAceptada;
    private javax.swing.JLabel lact;
    private javax.swing.JLabel lpost;
    private javax.swing.JLabel lpre;
    private javax.swing.JButton nextNFA;
    private javax.swing.JButton playNFA;
    private javax.swing.JButton previusNFA;
    // End of variables declaration//GEN-END:variables
}
